﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using nr.GLV.Windows.Controls;

namespace nr.GLV.Windows
{
    public partial class MainForm : System.Windows.Forms.Form
    {
        private nr.GLV.Data.nrDataSet Data { get { return nr.GLV.Data.DBO.Data; } }

        private void Quit()
        {
            System.Windows.Forms.Application.Exit();
        }

        private void PlaceControl(System.Windows.Forms.UserControl control)
        {
            ToolStripManager.RevertMerge(mnuMain);
            tscMain.ContentPanel.Controls.Clear();
            tscMain.ContentPanel.Controls.Add(control);
            control.Dock = System.Windows.Forms.DockStyle.Fill;
            control.Focus();
        }

        private void HandleRecordCountChanged(object sender, RecordCountChangedEventArgs e)
        {
            StatusMemo = string.Format("{0} : {1} records", e.ArchiveName, e.RecordCount);
        }

        private string StatusMemo { get; set; }

        public void ChangeStatus(string message)
        {
            StatusMemo = slInfo.Text;
            slInfo.Text = message;
        }

        public void RestoreStatus()
        {
            slInfo.Text = StatusMemo;
        }

        private void ShowCities()
        {
            ChangeStatus("Attendere...");
            CitiesDatagrid cities = new CitiesDatagrid();
            cities.RecordNumberChanged += new RecordCountChangedEventHandler(HandleRecordCountChanged);
            cities.Data = Data;
            PlaceControl(cities);
            RestoreStatus();
        }

        private void ShowCustomers()
        {
            ChangeStatus("Attendere...");
            CustomersDatagrid customers = new CustomersDatagrid();
            customers.RecordNumberChanged += new RecordCountChangedEventHandler(HandleRecordCountChanged);
            customers.Data = nr.GLV.Data.DBO.Data;
            PlaceControl(customers);
            RestoreStatus();
        }

        private void ShowGraves()
        {
            ChangeStatus("Attendere...");
            GravesDatagrid graves = new GravesDatagrid();
            graves.RecordNumberChanged += new RecordCountChangedEventHandler(HandleRecordCountChanged);
            graves.Data = nr.GLV.Data.DBO.Data;
            PlaceControl(graves);
            RestoreStatus();
        }

        private void ShowPayments()
        {
            ChangeStatus("Attendere...");
            PaymentsDatagrid payments = new PaymentsDatagrid();
            payments.RecordNumberChanged += new RecordCountChangedEventHandler(HandleRecordCountChanged);
            payments.Data = nr.GLV.Data.DBO.Data;
            PlaceControl(payments);
            RestoreStatus();
        }

        private bool UpdateDatabase()
        {
            try
            {
                Cursor = Cursors.WaitCursor;
                ChangeStatus("Attendere...");
                nr.GLV.Data.DBO.Update();
                return true;
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.Message);
            }
            finally
            {
                Cursor = Cursors.Default;
                RestoreStatus();
            }

            return false;
        }

        private void BackupData()
        {
            if (dlgBackup.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
                try
                {
                    Cursor = Cursors.WaitCursor;
                    ChangeStatus("Attendere...");
                    nr.GLV.Data.DBO.Backup(new System.IO.FileStream(dlgBackup.FileName, System.IO.FileMode.Create));
                }
                catch (Exception ex)
                {
                    System.Diagnostics.Debug.WriteLine(ex.Message);
                    MessageBox.Show(
                        "Impossibile effettuare il backup.\n\r" +
                        "Controllare di avere il permesso di scrivere sul file scelto.\n\r" +
                        "Se il problema persiste contattare l'assistenza.",
                        Application.ProductName,
                        MessageBoxButtons.OK,
                        MessageBoxIcon.Error);
                }
                finally
                {
                    RestoreStatus();
                    Cursor = Cursors.Default;
                }
        }

        private void RestoreData()
        {
            if (dlgRestore.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
                try
                {
                    Cursor = Cursors.WaitCursor;
                    ChangeStatus("Attendere...");
                    nr.GLV.Data.DBO.Restore(new System.IO.FileStream(dlgRestore.FileName, System.IO.FileMode.Open));
                }
                catch (Exception ex)
                {
                    System.Diagnostics.Debug.WriteLine(ex.Message);
                    MessageBox.Show(
                        "Impossibile recuperare i dati dal file specificato.\n\r" +
                        "Se il problema persiste contattare l'assistenza.",
                        Application.ProductName,
                        MessageBoxButtons.OK,
                        MessageBoxIcon.Error);
                }
                finally
                {
                    RestoreStatus();
                    Cursor = Cursors.Default;
                }
        }

        private void ToggleShortcutsWindow()
        {
            if (frmShortcuts.Visible)
                frmShortcuts.Hide();
            else
            {
                frmShortcuts.Location = PointToScreen(new System.Drawing.Point(miHelpShortcuts.Bounds.Left, miHelpShortcuts.Bounds.Bottom));
                frmShortcuts.Show();
                this.Focus();
            }
        }
    }
}
